package objects;

import java.util.ArrayList;

public class Inventory {
	
	public final int SIZE;
	public static final int MINE = 1;
	
	public static final int INVENTORY_BEGIN = 0x0;
	public static final int INVENTORY_END = 0x1;
	public static final int INVENTORY_ITEM = 0x2;
	
	private ArrayList<Integer> items = new ArrayList<Integer>();
	
	public Inventory(int size) {
		this.SIZE = size;
	}
	
	public boolean addItem(int item) {
		if (items.size() >= SIZE) {
			return false;
		}
		
		items.add(item);
		
		return true;
	}
	
	public boolean useItem(int itemID) {
		for (int i = 0; i < items.size(); i++) {
			if (items.get(i) == itemID) {
				items.remove(i);
				return true;
			}
		}
		
		return false;
	}
	
	public int[] getItemIDs() {
		int[] itemList = new int[SIZE];
		
		for (int i = 0; i < SIZE; i++) {
			if (i < items.size()) {
				itemList[i] = items.get(i);
			} else {
				itemList[i] = 0;
			}
		}
		
		return itemList;
	}
	
	@Override
	public String toString() {
		return items.toString();
	}
	
	public int createInventoryReport(int msgType, int digit) {
		int buffer = 1;
		buffer <<= 4;
		buffer |= msgType;
		buffer <<= 4;
		
		switch (msgType) {
		case INVENTORY_BEGIN:
			buffer |= SIZE;
			break;
		case INVENTORY_ITEM:
			if (digit < SIZE) {
				buffer |= items.get(digit);
			} else {
				return 0;
			}
		}
		
		return buffer;
	}
}
